<?php
/**
 * Author：ghostsf
 * Blog：www.ghostsf.com
 * Date: 2016/6/16
 */

namespace app\admin\controller;
use app\admin\model\Notice;
use app\admin\model\Campus;
use app\admin\validate\NoticeValidate;
use think\Db;
use think\File;
use think\Request;

/**
 * 公告
 * Class NoticeController
 * Author mww
 * @package app\admin\controller
 */
class LunboController extends CommonController
{
    /**
     * 获得数据
     */
    public function getDatas()
    {
        $rows = $_POST['rows'];
        $page = $_POST['page'];
        $where = array();
        if(isset($_POST['keyword']) && $_POST['keyword']!=''){
            $keyword = $_POST['keyword'];
            $where['lb_name'] = array('like',"%$keyword%");
        }
        $notice = Db::table('ew_lunbo')
            ->where($where)
            ->limit($rows * ($page - 1), $rows)
            ->order('lb_time DESC')
            ->select();
        $total = Db::table('ew_lunbo')
            ->where($where)
            ->count();
        $data['total'] = $total;
        $data['rows'] = $notice;
        return json_encode($data);
    }

    /**
     * enableLunbo 启用
     * @return 返回状态
     */
    public function enableLunbo(){
        $data = $this->oneuser($_POST['cid'],1,"已启用！","启用失败！");
        return json_encode($data);
    }

    /**
     * disabledLunbo 停用
     * @return 返回状态
     */
    public function disabledLunbo(){
        $data = $this->oneuser($_POST['cid'],2,"已停用！","停用失败！");
        return json_encode($data);
    }

    /**
     * oneuser 停用或启用
     * @param、$uid用户id、$num状态值、$succemsg成功返回信息、$failedmsg失败返回信息
     * @return 返回状态
     */
    protected function oneuser($uid,$num,$succemsg,$failedmsg){
        if($uid!=''){
            $where['lb_id'] = $uid;
            $data['lb_status'] = $num;//停用|启用
            $res =  Db::table('ew_lunbo')->where($where)->update($data);
            if($res){
                $returnData['status'] = 1;
                $returnData['msg'] = $succemsg;
            }else{
                $returnData['status'] = 0;
                $returnData['msg'] = $failedmsg;
            }
        }else{
            $returnData['status'] = 0;
            $returnData['msg'] = "传递参数错误";
        }
        return $returnData;
    }

    /**
     * Lunbo 删除轮播
     * @return 状态
     */
    public function deleteLunbo(){
        $id = $_POST['id'];
        /*删除旧图片*/
        $dir = Db::table('ew_lunbo')->where('lb_id',$id)->value('lb_image');
        $file = "static/uploads/images/".$dir;
        if(file_exists ($file)){
            unlink($file);
        }
        $res = Db::table('ew_lunbo')->delete($id);
        if($res){
            return json_encode(['status'=>1,'msg'=>"删除成功！"]);
        }else{
            return json_encode(['status'=>0,'msg'=>"删除失败！"]);
        }
    }


    /**
     * addLunbo 轮播添加展示页
     * @return HTML页面
     */
    public function addLunbo(){
        return $this->fetch('lunbo/add');
    }


    /**
     * add添加
     * @return 返回状态
     */
    public function add(){
        extract(input());
        if($lb_name==""){
            return json_encode(['status'=>0,'msg'=>"请填写轮播图名称！"]);
        }
        $data = [
            'lb_name'=>$lb_name,
            'lb_status'=>$lb_status,
            'lb_content'=>$con,
            'lb_time'=>date("Y-m-d H:i:s")
        ];
        $dir = $_SERVER['DOCUMENT_ROOT'];
        $url =  "http://".$_SERVER['HTTP_HOST'];
        if(!empty($_FILES['lb_image']['tmp_name'])){
            //=======重命名=====
            $rand = date('YmdHis').rand(100000,999999);
            $exe = explode('.',$_FILES['lb_image']['name'])[1];
            $_FILES['lb_image']['name'] = $rand . '.' .$exe;
            //=======================
            if(move_uploaded_file($_FILES['lb_image']['tmp_name'],"{$dir}/mang/image/{$_FILES['lb_image']['name']}")){
                $data['lb_image'] = "{$url}/mang/image/{$_FILES['lb_image']['name']}";
                if($_FILES['lb_image']['size'] > 1024*500) {
                    $this->image_png_size_add("$dir/mang/image/{$_FILES['lb_image']['name']}", "$dir/mang/image/{$_FILES['lb_image']['name']}");
                }
            }else{
                return json_encode(['status'=>0,'msg'=>'图片保存失败']);
            }
            $res = Db::table('ew_lunbo')->insert($data);
            if($res){
                return json_encode(['status'=>1,'msg'=>"添加成功！"]);
            }else{
                return json_encode(['status'=>0,'msg'=>"添加失败！"]);
            }
        }else{
            return json_encode(['status'=>0,'msg'=>"上传一张轮播图！"]);
        }
    }

    /**
     * editLunbo 展示轮播图修改页面
     * @return 返回HTML页面
     */
    public function editLunbo(){
        $data = Db::table('ew_lunbo')->where('lb_id',$_POST['id'])->find();
        $this->assign ('data',$data);
        return $this->fetch('lunbo/edit');
    }

    /**
     * edit 修改轮播图
     * @return 返回状态
     */
    public function edit(){
        extract(input());
        if($lb_name==""){
            return json_encode(['status'=>0,'msg'=>"请填写轮播图名称！"]);
        }
        $dir = $_SERVER['DOCUMENT_ROOT'];
        $url =  "http://".$_SERVER['HTTP_HOST'];
        $data = [
            'lb_name'=>$lb_name,
            'lb_status'=>$lb_status,
            'lb_content'=>$con,
            'lb_time'=>date("Y-m-d H:i:s")
        ];
        if(!empty($_FILES['lb_image']['tmp_name'])){
            //=======重命名=====
            $rand = date('YmdHis').rand(100000,999999);
            $exe = explode('.',$_FILES['lb_image']['name'])[1];
            $_FILES['lb_image']['name'] = $rand . '.' .$exe;
            //=======================
            if(move_uploaded_file($_FILES['lb_image']['tmp_name'],"{$dir}/mang/image/{$_FILES['lb_image']['name']}")){
                $data['lb_image'] = "{$url}/mang/image/{$_FILES['lb_image']['name']}";
            }else{
                return json_encode(['status'=>0,'msg'=>'图片保存失败']);
            }
        }

        $res = Db::table('ew_lunbo')->where('lb_id',$lb_id)->update($data);
        if($res){
            return json_encode(['status'=>1,'msg'=>"修改成功！"]);
        }else{
            if(file_exists ("static/uploads/images/".$new_image)){
                unlink("static/uploads/images/".$new_image);
            }
            return json_encode(['status'=>0,'msg'=>"修改失败！"]);
        }
    }

}